package com.todo.delicacy.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.delicacy.infrastructure.entity.condition.ContentFoodRecommendCondition;
import com.todo.delicacy.infrastructure.entity.po.ContentFoodRecommendPO;

import com.todo.delicacy.infrastructure.mapper.ContentFoodRecommendMapper;
import com.todo.delicacy.infrastructure.repository.ContentFoodRecommendRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ContentFoodRecommend)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("contentFoodRecommendRepository")
public class ContentFoodRecommendRepositoryImpl implements ContentFoodRecommendRepository {
    @Resource
    private ContentFoodRecommendMapper contentFoodRecommendMapper;


    @Override
    public PageResult<ContentFoodRecommendPO> queryContentFoodRecommendByCondition(ContentFoodRecommendCondition contentFoodRecommendCondition) {
        PageResult<ContentFoodRecommendPO> pageResult = new PageResult<>();
        if (contentFoodRecommendCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(contentFoodRecommendCondition.getPageIndex());
        pageResult.setPageSize(contentFoodRecommendCondition.getPageSize());
        long count = queryCountByCondition(contentFoodRecommendCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, contentFoodRecommendCondition.getPageSize()));
        pageResult.setTotal(count);
        List<ContentFoodRecommendPO> sightPoiPOList = contentFoodRecommendMapper.queryContentFoodRecommendByCondition(contentFoodRecommendCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= contentFoodRecommendCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(ContentFoodRecommendCondition contentFoodRecommendCondition) {
        if (contentFoodRecommendCondition == null || contentFoodRecommendCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return contentFoodRecommendMapper.queryCountByCondition(contentFoodRecommendCondition);
    }

    @Override
    public Boolean insertFoodContentRecommend(ContentFoodRecommendPO contentFoodRecommendPO) {
        if (contentFoodRecommendPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseCheckField(contentFoodRecommendPO)) {
            return false;
        }
        int saveResult = contentFoodRecommendMapper.insertContentFoodRecommendPO(contentFoodRecommendPO);
        return saveResult > 0;
    }


    @Override
    public Boolean updateFoodContentRecommend(ContentFoodRecommendPO contentFoodRecommendPO) {
        if (contentFoodRecommendPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseCheckField(contentFoodRecommendPO)) {
            return false;
        }
        int saveResult = contentFoodRecommendMapper.updateContentFoodRecommendPOById(contentFoodRecommendPO);
        return saveResult > 0;
    }

    private boolean baseCheckField(ContentFoodRecommendPO foodRecommendPO) {
        if (foodRecommendPO == null) {
            return false;
        }

        if (foodRecommendPO.getUserId() == null || foodRecommendPO.getFoodType() == null || foodRecommendPO.getRecommend() == null || foodRecommendPO.getProvince() == null || foodRecommendPO.getCity() == null) {
            log.warn("ContentFoodRecommendRepository#baseCheckField 推荐信息字段校验失败数据异常，操作失败 foodRecommendPO:【{}】", JSONObject.toJSONString(foodRecommendPO));
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean removeContentFoodRecommendPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentFoodRecommendPO contentFoodRecommendPO = queryContentFoodRecommendPOById(id);
        if (contentFoodRecommendPO == null) {
            log.info("ContentFoodRecommendRepository#removeContentFoodRecommendPOById 根据主键id查询美食内容推荐信息为空，默认删除成功 id:【{}】", id);
            return true;
        }
        int removeResult = contentFoodRecommendMapper.removeContentFoodRecommendPOById(id);
        return removeResult > 0;
    }

    @Override
    public ContentFoodRecommendPO queryContentFoodRecommendPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        ContentFoodRecommendPO contentFoodRecommendPO = contentFoodRecommendMapper.queryContentFoodRecommendPOById(id);
        return contentFoodRecommendPO;
    }

    @Override
    public ContentFoodRecommendPO queryContentFoodRecommendPOByContentId(Long contentId) {
        if (contentId == null || contentId < 0){
            return null;
        }
        return contentFoodRecommendMapper.queryContentFoodRecommendPOByContentId(contentId);
    }

}
